﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.ComponentModel;
using System.IO;
using System.ServiceModel;


namespace CMD_WEB.FileServer
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [MessageContract]
     public class FileUploader : System.Web.Services.WebService
    {
        //[MessageHeader(MustUnderstand = true)]
        //public string SavePath;

        //[MessageHeader(MustUnderstand = true)]
        //public string FileName;

        //[MessageBodyMember(Order = 1)]
        //public Stream FileData;
        //[WebMethod]
        //public static string UploadFile(byte[] f, string fileName)
        //{
        //    // the byte array argument contains the content of the file
        //    // the string argument contains the name and extension
        //    // of the file passed in the byte array
        //    try
        //    {
        //        // instance a memory stream and pass the
        //        // byte array to its constructor
        //        MemoryStream ms = new MemoryStream(f);

        //        // instance a filestream pointing to the
        //        // storage folder, use the original file name
        //        // to name the resulting file
        //        //HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data") 
        //        FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
        //                    ("~/FileServer") + fileName, FileMode.Create);

        //        // write the memory stream containing the original
        //        // file as a byte array to the filestream
        //        ms.WriteTo(fs);

        //        // clean up
        //        ms.Close();
        //        fs.Close();
        //        fs.Dispose();

        //        // return OK if we made it this far
        //        return "OK";
        //    }
        //    catch (Exception ex)
        //    {
        //        // return the error message if the operation fails
        //        return ex.Message.ToString();
        //    }
        //}

        [WebMethod]
        public byte[] ReturnBytesToClient(string FileName)
        {

            FileStream fs = null;
            //string FilePath = Server.MapPath("~") + "\\Archivos\\" + FileName;
            //string FilePath = "C:\\Users\\ISAAC\\Desktop\\Archivos\\" + FileName;
            string FilePath =@""+ System.Configuration.ConfigurationManager.AppSettings["RutaDestino"] + FileName;
            //C:\Users\ISAAC\Desktop\Archivos
            fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);

            byte[] byteBuffer = new byte[(int)fs.Length - 1]; //(byte[])(fs.Length-1) //((byte[])(fs.Length-1));
            fs.Read(byteBuffer, 0, 0);
            fs.Read(byteBuffer, 0, (int)fs.Length - 1);
            fs.Close();


            return byteBuffer;
        }
    }

       


}